﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "SampleGraphicsNodeStyleCustomizer.h"
#include "IElement.h"
#include "IDocument.h"
#include "IModelView.h"
#include "IGraphicsStyle.h"
#include "IDocumentManager.h"
#include "ILinkElementProxy.h"
#include "IGraphicsStyleData.h"
#include "ILinkDocumentElement.h"
#include "GraphicsNodeVisibility.h"
#include "IElementBasicInformation.h"
#include "IGraphicsNodeStyleAndMaterialCustomizer.h"

#include "CustomizerGStyleManager.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

//自定义显示样式定制
DBOBJECT_DATA_DEFINE(SampleGraphicsNodeStyleAndMaterialCustomizer)
{
    m_pOwnerElement = nullptr;
}

SampleGraphicsNodeStyleAndMaterialCustomizer::SampleGraphicsNodeStyleAndMaterialCustomizer(gcmp::IElement* pElement)
    : m_pOwnerElement(pElement)
{} 

gcmp::IGraphicsNodeStyleAndMaterialCustomizer::CustomizationInfo Sample::SampleGraphicsNodeStyleAndMaterialCustomizer::GetCustomizationInfo(const gcmp::IModelView * pModelView) const
{
    IGraphicsNodeStyleAndMaterialCustomizer::CustomizationInfo info;

    DBG_WARN_AND_RETURN_UNLESS(pModelView, info, L"pModelView为空",L"GDMP",L"2024-03-30");
    IElementBasicInformation* pElemBasicInfo = m_pOwnerElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_UNLESS(pElemBasicInfo, info, L"pElemBasicInfo为空",L"GDMP",L"2024-03-30");
    UniIdentity categoryId = pElemBasicInfo->GetCategoryUid();
    IDocument* pDocument = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDocument, info, L"pDocument为空",L"GDMP",L"2024-03-30");

    // 特别注意：IGraphicsNodeStyleAndMaterialCustomizer继承的类是不能包含任何显示样式Element数据的，因此显示样式的存储和管理全部在CustomizerGStyleManager
    CustomizerGStyleManager* pCustomizerGStyleManager = CustomizerGStyleManager::Get(pDocument);
    DBG_WARN_AND_RETURN_UNLESS(pCustomizerGStyleManager, info, L"pCustomizerGStyleManager为空",L"GDMP",L"2024-03-30");
    ElementId ownerElemId = GetOwnerElement()->GetElementId();
    ElementId viewId = pModelView->GetElementId();

    {   // 文档级图元显示样式重载
        ElementId gStyleId;
        pCustomizerGStyleManager->GetElementGStyle(ownerElemId, gStyleId);
        if (gStyleId.IsValid())
        {
            std::vector<ElementId> styleIds{ gStyleId };
            info.CustomizeElementGraphicsStyle(GraphicsStyleOverrideGrade::Document, GraphicsStyleActiveRange::Background, styleIds);
        }
    }
    {
        // 视图级图元显示样式重载
        ElementId gStyleId;
        pCustomizerGStyleManager->GetViewElementGStyle(ownerElemId, viewId, gStyleId);
        if (gStyleId.IsValid())
        {
            std::vector<ElementId> styleIds{ gStyleId };
            info.CustomizeElementGraphicsStyle(GraphicsStyleOverrideGrade::ModelView, GraphicsStyleActiveRange::Background, styleIds);
        }
    }
    {
        // 文档级图元的图形节点按照节点样式名称的显示样式重载
        const std::unordered_map<std::wstring, gcmp::ElementId, std::hash<std::wstring>>& map = pCustomizerGStyleManager->GetGNodeNameGStyles(ownerElemId);
        for(auto& iter : map)
        { 
            info.CustomizeElementGraphicsStyleByStyleName(GraphicsStyleOverrideGrade::Document, iter.first, iter.second);
        }
    }
    {
        // 文档级图元的图形节点按照节点样式类别的显示样式重载
        const std::unordered_map<gcmp::UniIdentity, gcmp::ElementId, gcmp::UniIdentityHash>& map = pCustomizerGStyleManager->GetGNodeCategoryGStyles(ownerElemId);
        for (auto& iter : map)
        {
            info.CustomizeElementGraphicsStyleByTargetCategoryUid(GraphicsStyleOverrideGrade::Document, iter.first, iter.second);
        }
    }
    {
        // 视图级图元的图形节点按照节点样式名称的显示样式重载
        const std::unordered_map<std::wstring, gcmp::ElementId, std::hash<std::wstring>>& map = pCustomizerGStyleManager->GetViewGNodeNameGStyles(ownerElemId, viewId);
        for (auto& iter : map)
        {
            info.CustomizeElementGraphicsStyleByStyleName(GraphicsStyleOverrideGrade::ModelView, iter.first, iter.second);
        }
    }
    {
        // 视图级图元的图形节点按照节点样式类别的显示样式重载
        const std::unordered_map<gcmp::UniIdentity, gcmp::ElementId, gcmp::UniIdentityHash>& map = pCustomizerGStyleManager->GetViewGNodeCategoryGStyles(ownerElemId, viewId);
        for (auto& iter : map)
        {
            info.CustomizeElementGraphicsStyleByTargetCategoryUid(GraphicsStyleOverrideGrade::ModelView, iter.first, iter.second);
        }
    }

    return info;
}

gcmp::IDocument * Sample::SampleGraphicsNodeStyleAndMaterialCustomizer::GetDocument()
{
    return m_pOwnerElement->GetDocument();
}

